package com.lagou.config;

import org.springframework.web.WebApplicationInitializer;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2021/3/10.
 */
@HandlesTypes(WebApplicationInitializer.class)
public class MySpringServletContainerInitializer implements ServletContainerInitializer {
    /**
     * Servlet 3.0+容器启动时将自动扫描类路径以查找实现Spring的Webapplicationinitializer接口的所有实现，
     * 将其放进一个Set集合中，提供给ServletContainerInitializer中onStartup方法的第一个参数。
     *
     * @param set
     * @param servletContext
     * @throws ServletException
     */
  //  @Override
    public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
        try {
            // 实例化所有 WebApplicationInitializer 的实现类
            List<WebApplicationInitializer> initializers = new ArrayList<WebApplicationInitializer>();
            if (set != null) {
                for (Class<?> initClass : set) {
                    if (!initClass.isInterface() && !Modifier.isAbstract( initClass.getModifiers() )) {
                        initializers.add( (WebApplicationInitializer) initClass.getConstructor().newInstance() );
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
